﻿package core.ui.util
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;

    public class BrushTuyitu extends Dialog
    {
        var bm:Bitmap;
        var shape:Brush;
        var type:BrushType;
        var circle:BrushTypeButton;
        var square:BrushTypeButton;
        var diamond:BrushTypeButton;
        var radie:Slidebar;
        var spikes:Slidebar;
        var hardness:Slidebar;
        var aspect:Slidebar;
        var angle:Slidebar;
        var step:Slidebar;
        var at:BrushTypeButton;
        var cx:int;
        var cy:int;
        var ap:int;
        var n:int;

        public function BrushTuyitu(param1:Tuyitu)
        {
            this.type = BrushType.Circle;
            super(param1, "rm_brush_editor", 430, 240, true);
            var _loc_2:* = new Sprite();
            _loc_2.graphics.beginFill(9474192);
            _loc_2.graphics.drawRect(0, 0, 155, 155);
            _loc_2.graphics.beginFill(16777215);
            _loc_2.graphics.drawRect(1, 1, 153, 153);
            _loc_2.graphics.endFill();
            _loc_2.x = 256;
            _loc_2.y = 35;
            addChild(_loc_2);
            this.bm = new Bitmap();
            this.bm.bitmapData = new BitmapData(150, 150, true, 16777215);
            var _loc_4:int = 2;
            this.bm.y = 2;
            this.bm.x = _loc_4;
            _loc_2.addChild(this.bm);
            var _loc_3:* = new Literal("rm_type", true);
            _loc_3.x = 18;
            _loc_3.y = 37;
            _loc_3.width = 80;
            addChild(_loc_3);
            this.circle = new BrushTypeButton(BrushType.Circle, new Circle(), 4, 4);
            this.circle.addEventListener(MouseEvent.MOUSE_DOWN, this.TypeMouseDown, false, 1, true);
            this.circle.x = 100;
            this.circle.y = 35;
            this.at = this.circle;
            this.at.Mark(true);
            addChild(this.circle);
            this.square = new BrushTypeButton(BrushType.Square, new BitmapData(12, 12, false, 0), 6, 6);
            this.square.addEventListener(MouseEvent.MOUSE_DOWN, this.TypeMouseDown, false, 1, true);
            this.square.x = 130;
            this.square.y = 35;
            addChild(this.square);
            this.diamond = new BrushTypeButton(BrushType.Diamond, new Diamond(), 5, 5);
            this.diamond.addEventListener(MouseEvent.MOUSE_DOWN, this.TypeMouseDown, false, 1, true);
            this.diamond.x = 160;
            this.diamond.y = 35;
            addChild(this.diamond);
            this.radie = new Slidebar("rm_radie", 0, 250, 100, 20, true, true);
            this.radie.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.radie.addEventListener(SliderEvent.CHANGE, this.SetStepChange, false, 1, true);
            this.radie.x = 100;
            this.radie.y = 60;
            addChild(this.radie);
            this.hardness = new Slidebar("rm_hardness", 0, 100, 100, 85, true, false);
            this.hardness.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.hardness.addEventListener(SliderEvent.CHANGE, this.SetStepChange, false, 1, true);
            this.hardness.x = 100;
            this.hardness.y = 82;
            addChild(this.hardness);
            this.aspect = new Slidebar("rm_aspect", 1, 20, 100, 1, true, true);
            this.aspect.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.aspect.x = 100;
            this.aspect.y = 104;
            addChild(this.aspect);
            this.spikes = new Slidebar("rm_spikes", 2, 20, 100, 2, true, false);
            this.spikes.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.spikes.x = 100;
            this.spikes.y = 126;
            addChild(this.spikes);
            this.angle = new Slidebar("rm_angle", 0, 180, 100, 0, true, true);
            this.angle.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.angle.x = 100;
            this.angle.y = 148;
            addChild(this.angle);
            this.step = new Slidebar("rm_spacing", 0, 200, 100, 0, true, false);
            this.step.addEventListener(SliderEvent.DRAG, this.Change, false, 1, true);
            this.step.x = 100;
            this.step.y = 170;
            addChild(this.step);
            this.Change();
            return;
        }// end function

        private function TypeMouseDown(event:MouseEvent) : void
        {
            this.at.Mark(false);
            this.at = event.currentTarget as BrushTypeButton;
            this.at.Mark(true);
            this.type = this.at.type;
            this.Change();
            return;
        }// end function

        private function Change(... args) : void
        {
            args = 0;
            var _loc_3:int = 0;
            this.shape = Misc.GenerateBrush(new ShapeDetails(this.type, this.radie.value > 73.5 ? (73.5) : (this.radie.value), this.spikes.value, this.hardness.value / 100, this.aspect.value, this.angle.value, this.step.value));
            this.bm.bitmapData.lock();
            this.bm.bitmapData.fillRect(this.bm.bitmapData.rect, 16777215);
            this.cx = Math.round((150 - this.shape.width) / 2);
            this.cy = Math.round((150 - this.shape.height) / 2);
            this.n = -1;
            while (_loc_3 < this.shape.height)
            {
                
                args = 0;
                while (args < this.shape.width)
                {
                    
                    var _loc_4:String = this;
                    _loc_4.n = this.n + 1;
                    this.ap = this.shape.data[++this.n];
                    if (this.ap > 0)
                    {
                        this.bm.bitmapData.setPixel32(this.cx + args, this.cy + _loc_3, this.ap << 24 | 0 << 16 | 0 << 8 | 0);
                    }
                    args++;
                }
                _loc_3++;
            }
            this.bm.bitmapData.unlock();
            return;
        }// end function

        private function SetStepChange(... args) : void
        {
            this.step.Set(Math.round(this.radie.value / 5 + this.radie.value / 5 * (this.hardness.value / 100)));
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            ed.options.brushes.Add(Misc.GenerateBrush(new ShapeDetails(this.type, this.radie.value, this.spikes.value, this.hardness.value / 100, this.aspect.value, this.angle.value, this.step.value)));
            Close();
            return;
        }// end function

    }
}
